<?php
require_once 'common.inc.php';

foreach($_POST as $_key => $_value) {
	!ereg("^\_", $_key) && $$_key = $_POST[$_key];
} 
foreach($_GET as $_key => $_value) {
	!ereg("^\_", $_key) && $$_key = $_GET[$_key];
}

if(empty($action))$action='list';
if($action=='list'){
	$db->query("SET NAMES utf8");
	$sql="select * from msys_client";
	$query = $db -> query($sql);
	while($list=$db -> fetch_array($query)){
		$result[]=$list;
	}
	foreach($result as $value){
		switch($value['sort']){
			case'Telecom':
				for($x=0;$x<count($result);$x++){
				if(isset($client[$x]['Telecom'])){
					continue;
				}else{
					$client[$x]['Telecom']=$value['name'];
					break;
				}
			}
			break;			
			case'Netcom':
				for($x=0;$x<count($result);$x++){
				if(isset($client[$x]['Netcom'])){
					continue;
				}else{
					$client[$x]['Netcom']=$value['name'];
					break;
				}
			}
			break;
			case'Great Wall':
				for($x=0;$x<count($result);$x++){
				if(isset($client[$x]['Great Wall'])){
					continue;
				}else{
					$client[$x]['Great Wall']=$value['name'];
					break;
				}
			}
			break;
			case'Other':
				for($x=0;$x<count($result);$x++){
				if(isset($client[$x]['Other'])){
					continue;
				}else{
					$client[$x]['Other']=$value['name'];
					break;
				}
			}
			break;
		}
	}
	$CreatHtml=CreatHtml($client);
	//header("Content-type: text/html; charset=UTF-8");
	include './templates/msystem/clientlist.html';
}elseif($action=='status'){
	$name=urldecode($name);
	$trans=array('Telecom'=>'电信','Netcom'=>'网通','Great Wall'=>'长城宽带','Other'=>'其他');

	$sql="select link,sort from msys_client where name='$name'";
	$query=$db->query($sql);
	$result=$db->fetch_array($query);
	$link=$result['link']."./push/log/switch.php";
	$sort=$result['sort'];
	$link2=$result['link']."./controlcc/syslevel.php";
	$link3=$result['link']."./controlcc/alarm.php";

	$content=file_get_contents($link);
	$content2=file_get_contents($link2);
	$content3=file_get_contents($link3);
	
	$status=explode(',',$content);
	$status2=explode(':',$content2);
	$status3=explode(',',$content2);


	$switch=array('1'=>'是','2'=>'否','3'=>'透明','4'=>'推送页','9'=>'是','10'=>'否','11'=>'是','12'=>'否');
	$level=array('1'=>'级别一','2'=>'级别二','3'=>'级别三','4'=>'强制级别');


	include './templates/msystem/clientstatus.html';
}elseif($action=='add'){

	$sql="insert into msys_client (name,link,sort) values('$name','$link','$sort')";
	$db->query($sql);
	adminmsg('客户添加成功', 'client.php?action=list');

}elseif($action=='edit'){
	$action='oedit';
	$sql="select * from msys_client where name='$name'";
	$query=$db->query($sql);
	$result=$db->fetch_array($query);
	include './templates/msystem/clientedit.html';

}elseif($action=='oedit'){
	$sql="update msys_client set name='$name',link='$link',sort='$sort' where id='$id'";
	$query=$db->query($sql);
	adminmsg('客户属性修改成功', 'client.php?action=list');
}


function CreatHtml($array){
	$i=0;
	foreach($array as $list){
		if(isset($list['Telecom'])){
			$listlink['Telecom']=urlencode($list['Telecom']);
			$ClientHtml[$i]['Telecom']="<a href=\"client.php?action=status&amp;name={$listlink['Telecom']}\">{$list['Telecom']}</a>&nbsp;<img src=\"./images/normal.jpg\">&nbsp;";
		}
		if(isset($list['Netcom'])){
			$listlink['Netcom']=urlencode($list['Netcom']);
			$ClientHtml[$i]['Netcom']="<a href=\"client.php?action=status&amp;name={$listlink['Netcom']}\">{$list['Netcom']}</a>&nbsp;<img src=\"./images/normal.jpg\">&nbsp;";
		}
		if(isset($list['Great Wall'])){
			$listlink['Great Wall']=urlencode($list['Great Wall']);
			$ClientHtml[$i]['Great Wall']="<a href=\"client.php?action=status&amp;name={$listlink['Great Wall']}\">{$list['Great Wall']}</a>&nbsp;<img src=\"./images/normal.jpg\">&nbsp;";
		}
		if(isset($list['Other'])){
			$listlink['Other']=urlencode($list['Other']);
			$ClientHtml[$i]['Other']="<a href=\"client.php?action=status&amp;name={$listlink['Other']}\">{$list['Other']}</a>&nbsp;<img src=\"./images/normal.jpg\">&nbsp;";
		}
		$i++;
	}
	return $ClientHtml;
}
?>